package com.grupoconstruobras.sigo.configuracion.parametrizacion;

import com.grupoconstruobras.sigo.core.config.IParamConfigAppBussines;
import com.grupoconstruobras.sigo.exhumaciones.ui.datamodel.DataModelMb;
import com.grupoconstruobras.sigo.exhumaciones.ui.datamodel.IDataModelMb;
import com.grupoconstruobras.sigo.exhumaciones.ui.datamodel.NativeDataModelFactory;
import com.grupoconstruobras.sigo.ServiceException;

import java.io.Serializable;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.richfaces.component.SortOrder;

import com.google.common.collect.Maps;

@ViewScoped
@Named("consultarParametrosConfig")
public class ConsultaParamConfig implements Serializable{
	
	private static final long serialVersionUID = 1L;

	private final Logger LOG = Logger.getLogger(this.getClass().getName());
	
	@PersistenceContext(unitName="default")
	private EntityManager entityManager;
	
	// Bussines
	@Inject
	protected IParamConfigAppBussines paramConfigBussines;
	
	private IDataModelMb<Object []> paramConfigAppConsulta;
	
	private Map<String, SortOrder> paramConfgOrders = Maps.newHashMapWithExpectedSize(1);
	
	private Map<String, Object> paramConfgFilters = Maps.newHashMap();

	private Object casoSeleccionado;

	private Long idParamEliminar;
	
	@PostConstruct
	public void init() {
		inicializarDataModel();
	}

	public void inicializarDataModel(){
				
		paramConfgOrders.put("parametro", SortOrder.unsorted);
		paramConfgOrders.put("descripcion", SortOrder.unsorted);
		paramConfgOrders.put("tipodato", SortOrder.unsorted);
		paramConfgOrders.put("valor", SortOrder.unsorted);

		paramConfgFilters.put("parametro", null);
		paramConfgFilters.put("descripcion", null);
		paramConfgFilters.put("tipodato", null);
		paramConfgFilters.put("valor", null);
		
		paramConfigAppConsulta = new DataModelMb<Object[]>(paramConfgFilters, paramConfgOrders,
				new NativeDataModelFactory(entityManager,
				new ParamConfigAppQuery()));
		
	}
	
	public void cancelar(){
		init();		
	}

	public String crear(){
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("action", "insertar");
		return "detParamConfigSistema";
	}
	
	public String verDetalle(){
		
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("parametroId", this.casoSeleccionado+"");
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("action", "view");
	    
	    return "detParamConfigSistema";
	}
	
	public void eliminarParametro(){
		try {
			paramConfigBussines.delete(this.idParamEliminar);
//			paramConfigBussines.delete(Long.valueOf(this.casoSeleccionado.toString()));
		} catch (ServiceException e) {
			LOG.severe(e.getMessage());
		}
		init();		
	}
	
	public String editarParametro(){
		
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("parametroId", this.casoSeleccionado+"");
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("action", "edit");
	    
	    return "detParamConfigSistema";
	}
	
	public IDataModelMb<Object[]> getParamConfigAppConsulta() {
		return paramConfigAppConsulta;
	}

	public void setParamConfigAppConsulta(IDataModelMb<Object[]> paramConfigAppConsulta) {
		this.paramConfigAppConsulta = paramConfigAppConsulta;
	}

	public Map<String, SortOrder> getParamConfgOrders() {
		return paramConfgOrders;
	}

	public void setParamConfgOrders(Map<String, SortOrder> paramConfgOrders) {
		this.paramConfgOrders = paramConfgOrders;
	}

	public Map<String, Object> getParamConfgFilters() {
		return paramConfgFilters;
	}

	public void setParamConfgFilters(Map<String, Object> paramConfgFilters) {
		this.paramConfgFilters = paramConfgFilters;
	}

	public Object getCasoSeleccionado() {
		return casoSeleccionado;
	}

	public void setCasoSeleccionado(Object casoSeleccionado) {
		this.casoSeleccionado = casoSeleccionado;
	}

	public Long getIdParamEliminar() {
		return idParamEliminar;
	}

	public void setIdParamEliminar(Long idParamEliminar) {
		this.idParamEliminar = idParamEliminar;
	}

}
